package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtils {

    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    private String url;

    private String virtualHost;

    private String ssoTokenKey;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getVirtualHost() {
        return virtualHost;
    }

    public void setVirtualHost(String virtualHost) {
        this.virtualHost = virtualHost;
    }

    public String getSsoTokenKey() {
        return ssoTokenKey;
    }

    public void setSsoTokenKey(String ssoTokenKey) {
        this.ssoTokenKey = ssoTokenKey;
    }

    public void invokeHttpMethod(Map<String, String> params) throws HttpException, IOException {
        HttpClient client = new HttpClient();

        HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
        // 设置连接超时时间(单位毫秒)
        managerParams.setConnectionTimeout(10000);
        // 设置读数据超时时间(单位毫秒)
        managerParams.setSoTimeout(30000);

        PostMethod post = new PostMethod(url);
        HttpMethodParams p = new HttpMethodParams();
        if (virtualHost == null || "".equals(virtualHost)) {
            virtualHost = ".hn12396.com";
        }
        p.setVirtualHost(getVirtualHost());

        post.setParams(p);
        NameValuePair[] pairs = null;
        if (null != params) {
            int size = params.size();
            // NameValuePair[] postParam = new NameValuePair[size];
            List<NameValuePair> parame = new ArrayList<NameValuePair>();
            Set<String> keys = params.keySet();
            for (String key : keys) {
                NameValuePair param = new NameValuePair(key, params.get(key));
                parame.add(param);
            }
            if (parame != null && parame.size() > 0) {
                pairs = parame.toArray(new NameValuePair[size]);
            }
            post.setRequestBody(pairs);
        }

        int status = client.executeMethod(post);
        log.info("单点登陆方法调用返回状态：" + status);
        String responseBodyAsString = post.getResponseBodyAsString();
        System.out.println(responseBodyAsString);
//        String s = responseBodyAsString;
        Header location = post.getRequestHeader("Location");
        if (location != null)
            System.out.println("Created new booking at :" + location.getValue());

//        Header[] headers = post.getRequestHeaders();
//        for (Header header : headers) {
//            System.out.println(header.getName() + "\t" + header.getValue());
//        }
    }

    public static void main(String[] args) throws HttpException, IOException {
        String url = "http://localhost:8080/gtcms/authSSO.jhtml";
        Map<String, String> param = new HashMap<String, String>();
        param.put("username","admin");
        param.put("password","surekam@12");
        HttpUtils http = new HttpUtils();
        http.setUrl(url);
        http.invokeHttpMethod(param);
        
//        String cok = "http://www.hn12396.com/authentication.jhtml";
//        Map<String,String> p = new HashMap<String, String>();
//        p.put("cookies", "So0e8we3NkLzskKqpBffbhmhCfPwsfSfDxPQ+6CpDgTvhCbTk/FsaiT9b/AXkSTomRFMJFLoqw8m\nuBXHjZmZRQ==");
//        HttpUtils h = new HttpUtils();
//        h.setUrl(cok);
//        h.invokeHttpMethod(p);
    }
}
